package com.edu.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.edu.msmservice.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @ClassName MsmServiceImpl
 * @date 2022/9/22 15:29
 * @Version 1.0
 */
@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public Boolean send(Map<String, Object> map, String phone) {

        if(StringUtils.isEmpty(map)) return false;
        //创建对象
        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI5tMAhGhroppGFcDvzNNK", "52Y4gVka2DBkISM42oDh8jEE01sNp6");
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关的固定参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //设置手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //设置签名
        request.putQueryParameter("SignName", "内测青橙");
        //设置模板编号
        request.putQueryParameter("TemplateCode", "SMS_185811968");
        //设置发送的验证码 有map是为了验证码好返回值
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
